package com.hl.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.entity.SysConsultantAreaRel;
import com.hl.sfzy.entity.SysConsultants;
import com.hl.sfzy.entity.SysUser;
import com.hl.sfzy.mapper.SysConsultantAreaRelMapper;
import com.hl.sfzy.mapper.SysConsultantsMapper;
import com.hl.sfzy.pojo.FormDate;
import com.hl.sfzy.mapper.SysUserMapper;
import com.hl.sfzy.service.SysConsultantsService;
import com.hl.sfzy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("sysConsultantsService")
public class SysConsultantsServiceImpl implements SysConsultantsService {
    @Autowired
    private SysConsultantsMapper sysConsultantsMapper;
    @Autowired
    private SysConsultantAreaRelMapper sysConsultantAreaRelMapper;
    @Autowired
    private SysConsultantAreaRel sysConsultantAreaRel;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUser sysUser;
    @Override
    public int add(SysConsultants entity) {
        int num = sysConsultantsMapper.insert(entity);
        sysConsultantAreaRel.setConsultantId(entity.getId());
        for (Integer id: entity.getAreaOfExpertiseId()) {
            sysConsultantAreaRel.setAreaOfExpertiseId(id);
            sysConsultantAreaRelMapper.insert(sysConsultantAreaRel);
        }
        sysUser.setUsername(entity.getMobile());
        sysUser.setMobile(entity.getMobile());
        sysUser.setNickname(entity.getConsultantName());
        sysUser.setEmail(entity.getEmail());
        sysUser.setUserpwd("123456");
        sysUserService.add(sysUser);
        return num;
}

    @Override
    public int edit(SysConsultants entity) {
        Integer[] integers = sysConsultantAreaRelMapper.selectByConsultId(entity.getId());
        sysConsultantAreaRelMapper.deleteBatch(integers);
        sysConsultantAreaRel.setConsultantId(entity.getId());
        for (Integer id: entity.getAreaOfExpertiseId()) {
            sysConsultantAreaRel.setAreaOfExpertiseId(id);
            sysConsultantAreaRelMapper.insert(sysConsultantAreaRel);
        }

        return sysConsultantsMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        return sysConsultantsMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return sysConsultantsMapper.deleteBatch(ids);
    }

    @Override
    public List<SysConsultants> find(SysConsultants entity) {
        return sysConsultantsMapper.select(entity);
    }

    @Override
    public SysConsultants findById(Integer id) {
        return sysConsultantsMapper.selectById(id);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, SysConsultants entity) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //当前页数据
        List<SysConsultants> list = sysConsultantsMapper.select(entity);
        //分页信息对象
        PageInfo<SysConsultants> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        //map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST,list);
        map.put(CommonConstant.PAGE_TOTAL,pageInfo.getPages());
        map.put(CommonConstant.TOTAL,pageInfo.getTotal());
        return map;
    }


    @Override
    public List<SysConsultants> findByPosition(String position) {
        return sysConsultantsMapper.selectByPosition(position);
    }
    @Override
    public List<FormDate> findByLevelName(FormDate formDate) {
        return sysConsultantsMapper.selectByLevelName(formDate);
    }
    public int findByPhoto(Integer id) {
        return sysConsultantsMapper.selectByPhoto(id);
    }
    public SysConsultants findInfo(Integer id) {
        return sysConsultantsMapper.selectInfo(id);
    }

}
